package com.test.demo;

import java.util.List;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;

public class WiFiAdnim {
	
	private WifiManager mWifiManager;
	private WifiInfo mWifiInfo;
	private List<ScanResult> mWifiList;
	private List<WifiConfiguration> mWifiConfigurations;
	
	WifiLock mWifiLock;
	
	public WiFiAdnim(Context context)
	{
		//ȡ�õ�mWifiManager����
		mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		//ȡ��WifiInfo����
		mWifiInfo = mWifiManager.getConnectionInfo();
		
	}

	public void openWifi()
	{
		if(!mWifiManager.isWifiEnabled())
		{
			mWifiManager.setWifiEnabled(true);
			
		}
	}
	

	public void closeWifi()
	{
		if(mWifiManager.isWifiEnabled())
		{
			mWifiManager.setWifiEnabled(false);
		}
	}
	

	public int checkState()
	{
		return mWifiManager.getWifiState();
	}


	public void acquireWifiLock()
	{
			mWifiLock.acquire();
	}
	

	public void releaseWifiLock()
	{
		if(mWifiLock.isHeld())
		{
			mWifiLock.acquire();
		}
	}
	

	public void createWifiLock()
	{
		mWifiLock = mWifiManager.createWifiLock("test");
	}
	

	public List<WifiConfiguration> getConfiguration()
	{
		return mWifiConfigurations;
	}
	

	public void connetionConfiguration(int index)
	{
		if(index > mWifiConfigurations.size())
		{
			return;
		}

		mWifiManager.enableNetwork(mWifiConfigurations.get(index).networkId, true);
	}
	

	public void startScan(){

		mWifiManager.startScan();

		mWifiList = mWifiManager.getScanResults();

		mWifiConfigurations = mWifiManager.getConfiguredNetworks();
		
		
	}
	

	public List<ScanResult> getWifiList()
	{
		return mWifiList;
	}
	

	public StringBuilder lookUpScan()
	{
		StringBuilder sb = new StringBuilder();
			for(int i=0; i<mWifiList.size(); i++)
			{
				sb.append("Index_"+ new Integer(i+1).toString()+":");

				sb.append((mWifiList.get(i)).toString()).append("\n");
			}
			return sb;
	}
	

	public String getMacAddress()
	{  
		   return (mWifiInfo == null)?"NULL":mWifiInfo.getMacAddress();  
	} 
	

	public String getBSSID()
	{  
		   return (mWifiInfo == null)?"NULL":mWifiInfo.getBSSID();  
	}  
	

	public int getIpAddress()
	{  
		   return (mWifiInfo == null)?0:mWifiInfo.getIpAddress();  
	}

	public int getNetWordId()
	{
		return (mWifiInfo == null)?0:mWifiInfo.getNetworkId();
	}

	public String getWifiInfo()
	{
		return(mWifiInfo == null)?"NULL":mWifiInfo.toString();
	}
	

	public void addNetWork(WifiConfiguration configuration)
	{
		int wcgId = mWifiManager.addNetwork(configuration);
		mWifiManager.enableNetwork(wcgId, true);
	}

	public void disConnectionWifi(int netId)
	{
		mWifiManager.disableNetwork(netId);
		mWifiManager.disconnect();
	}
	

	public WifiConfiguration CreateWifiInfo(String SSID, String Password, int Type)   
	    {   
	          WifiConfiguration config = new WifiConfiguration();     
	           config.allowedAuthAlgorithms.clear();   
	           config.allowedGroupCiphers.clear();   
	           config.allowedKeyManagement.clear();   
	           config.allowedPairwiseCiphers.clear();   
	           config.allowedProtocols.clear();   
	           config.SSID = "\"" + SSID + "\"";     
	            
	              WifiConfiguration tempConfig = this.IsExsits(SSID);             
	          if(tempConfig != null) {    
	              mWifiManager.removeNetwork(tempConfig.networkId);    
	          }  
	            
	          if(Type == 1) //WIFICIPHER_NOPASS  
	          {   
	              // config.wepKeys[0] = "";
	               config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
	              // config.wepTxKeyIndex = 0;   
	          }   
	          if(Type == 2) //WIFICIPHER_WEP  
	          {   
	              config.hiddenSSID = true;  
	              config.wepKeys[0]= "\""+Password+"\"";   
	              config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);   
	              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);   
	              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);   
	              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);   
	              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);   
	              config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);   
	              config.wepTxKeyIndex = 0;   
	          }   
	          if(Type == 3) //WIFICIPHER_WPA  
	          {   
	          config.preSharedKey = "\""+Password+"\"";   
	          config.hiddenSSID = true;     
	          config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);     
	          config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);                           
	          config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);                           
	          config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);                      
	          //config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);    
	          config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);  
	          config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);  
	          config.status = WifiConfiguration.Status.ENABLED;  
	          
	          }  
	           return config;   
	    }   
	      
	    private WifiConfiguration IsExsits(String SSID)    
	    {    
	        List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();    
	           for (WifiConfiguration existingConfig : existingConfigs)     
	           {    
	             if (existingConfig.SSID.equals("\""+SSID+"\""))    
	             {    
	                 return existingConfig;    
	             }    
	           }    
	        return null;     
	    }   

}










